package com.yt.sys.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.sys.common.config.YmlConfig;
import com.yt.sys.mapper.SysRoleMapper;
import com.yt.sys.models.bo.SysPermissionSaveBO;
import com.yt.sys.models.bo.SysRoleSaveBO;
import com.yt.sys.models.dto.SysRoleDTO;
import com.yt.sys.models.dto.SysRolePermissionDTO;
import com.yt.sys.models.service.ISysRoleService;
import com.yt.sys.models.vo.SysRoleVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统角色信息控制器
 *
 * @author zhou
 * @Date 2025-09-03 14:47:27
 */
@DubboService
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleDTO> implements ISysRoleService {

    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private SysRolePermissionServiceImpl sysRolePermissionService;

    @Override
    public boolean add(SysRoleSaveBO saveBO) {
        SysRoleDTO dto = new SysRoleDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    @Transactional
    public boolean roleAdd(SysRoleSaveBO saveBO) {
        //1.插入角色信息
        this.add(saveBO);
        //2.插入角色权限信息
        SysRolePermissionDTO dto = new SysRolePermissionDTO();
        for (SysPermissionSaveBO bo : saveBO.getSysPermissionList()) {
            SysRolePermissionDTO rolePermissionDTO = new SysRolePermissionDTO();
            BeanUtils.copyProperties(bo, rolePermissionDTO);
            rolePermissionDTO.setId(ymlConfig.getPkId());
            rolePermissionDTO.setRoleId(saveBO.getId());
        }

        return false;
    }

    @Override
    public List<SysRoleVO> queryRole() {
        List<SysRoleVO> vo = baseMapper.query();
        return vo;
    }
}
